package wiki.douzi.helpyougrow.controller;

import wiki.douzi.helpyougrow.domain.Goods;
import wiki.douzi.helpyougrow.domain.GoodsOrder;
import wiki.douzi.helpyougrow.service.GoodsOrderService;
import wiki.douzi.helpyougrow.enumeration.ResponseCodeEnum;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;

/**
 * (GoodsOrder)表控制层
 *
 * @author Douzi
 * @since 2022-06-18 16:39:18
 */
@RestController
@RequestMapping("goodsOrder")
public class GoodsOrderController {
    /**
     * 服务对象
     */
    @Resource
    private GoodsOrderService goodsOrderService;

    /**
     * 查询分页数据
     *
     * @return 实例对象集合
     */
    @GetMapping("/page")
    public ResponseEntity<Map<String, Object>> queryAllByPage(
            @RequestParam(name = "page") String rawPage,
            @RequestParam(name = "limit") String rawLimit
    ) {
        Map<String, Object> resultMap = new HashMap<>();
        List<GoodsOrder> data = this.goodsOrderService.queryAll();
        int page = Integer.parseInt(rawPage);
        int limit = Integer.parseInt(rawLimit);
        List<GoodsOrder> subList = data.stream().skip((page - 1) * limit).limit(limit).
                collect(Collectors.toList());
        if (subList != null) {
            resultMap.put("msg", ResponseCodeEnum.SUCCESS.getMsg());
            resultMap.put("code", ResponseCodeEnum.SUCCESS.getCode());
            resultMap.put("data", subList);
            resultMap.put("count", data.size());
        } else {
            resultMap.put("msg", ResponseCodeEnum.SYSTEM_ERROR.getMsg());
            resultMap.put("code", ResponseCodeEnum.SYSTEM_ERROR.getCode());
        }
        return new ResponseEntity<Map<String, Object>>(resultMap, HttpStatus.OK);
    }

    /**
     * 查询所有数据
     *
     * @return 实例对象集合
     */
    @GetMapping
    public ResponseEntity<Map<String, Object>> queryAll(
            @RequestParam(name = "page") String page,
            @RequestParam(name = "limit") String limit
    ) {
        Map<String, Object> resultMap = new HashMap<>();
        List<GoodsOrder> data = this.goodsOrderService.queryAll();
        if (data != null) {
            resultMap.put("msg", ResponseCodeEnum.SUCCESS.getMsg());
            resultMap.put("code", ResponseCodeEnum.SUCCESS.getCode());
            resultMap.put("data", data);
        } else {
            resultMap.put("msg", ResponseCodeEnum.SYSTEM_ERROR.getMsg());
            resultMap.put("code", ResponseCodeEnum.SYSTEM_ERROR.getCode());
        }
        return new ResponseEntity<Map<String, Object>>(resultMap, HttpStatus.OK);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Map<String, Object>> queryById(@PathVariable("id") Integer id) {
        Map<String, Object> resultMap = new HashMap<>();
        GoodsOrder data = this.goodsOrderService.queryById(id);
        if (data != null) {
            resultMap.put("msg", ResponseCodeEnum.SUCCESS.getMsg());
            resultMap.put("code", ResponseCodeEnum.SUCCESS.getCode());
            resultMap.put("data", data);
        } else {
            resultMap.put("msg", ResponseCodeEnum.SELECT_NULL.getMsg());
            resultMap.put("code", ResponseCodeEnum.SELECT_NULL.getCode());
        }
        return new ResponseEntity<Map<String, Object>>(resultMap, HttpStatus.OK);
    }

    /**
     * 新增数据
     *
     * @param goodsOrder 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> add(@RequestBody GoodsOrder goodsOrder) {
        Map<String, Object> resultMap = new HashMap<>();
        GoodsOrder data = this.goodsOrderService.insert(goodsOrder);
        if (data != null) {
            resultMap.put("msg", ResponseCodeEnum.INSERT_SUCCESS.getMsg());
            resultMap.put("code", ResponseCodeEnum.INSERT_SUCCESS.getCode());
            resultMap.put("data", data);
        } else {
            resultMap.put("msg", ResponseCodeEnum.SYSTEM_ERROR.getMsg());
            resultMap.put("code", ResponseCodeEnum.SYSTEM_ERROR.getCode());
        }
        return new ResponseEntity<Map<String, Object>>(resultMap, HttpStatus.OK);
    }

    /**
     * 编辑数据
     *
     * @param goodsOrder 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Map<String, Object>> edit(@RequestBody GoodsOrder goodsOrder) {
        Map<String, Object> resultMap = new HashMap<>();
        GoodsOrder data = this.goodsOrderService.update(goodsOrder);
        if (data != null) {
            resultMap.put("msg", ResponseCodeEnum.MODIFY_SUCCESS.getMsg());
            resultMap.put("code", ResponseCodeEnum.MODIFY_SUCCESS.getCode());
            resultMap.put("data", data);
        } else {
            resultMap.put("msg", ResponseCodeEnum.SYSTEM_ERROR.getMsg());
            resultMap.put("code", ResponseCodeEnum.SYSTEM_ERROR.getCode());
        }
        return new ResponseEntity<Map<String, Object>>(resultMap, HttpStatus.OK);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Map<String, Object>> deleteById(Integer id) {
        Map<String, Object> resultMap = new HashMap<>();
        boolean data = this.goodsOrderService.deleteById(id);
        if (data) {
            resultMap.put("msg", ResponseCodeEnum.DELETE_SUCCESS.getMsg());
            resultMap.put("code", ResponseCodeEnum.DELETE_SUCCESS.getCode());
        } else {
            resultMap.put("msg", ResponseCodeEnum.SYSTEM_ERROR.getMsg());
            resultMap.put("code", ResponseCodeEnum.SYSTEM_ERROR.getCode());
        }
        return new ResponseEntity<Map<String, Object>>(resultMap, HttpStatus.OK);
    }

}


